#include <QDir>
#include <QTextStream>
#include <QFileDialog>

#include "WidgetConfiguration.h"
#include "ui_WidgetConfiguration.h"

WidgetConfiguration::WidgetConfiguration(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::WidgetConfiguration)
{
    m_ui->setupUi(this);
}


WidgetConfiguration::~WidgetConfiguration()
{
    delete m_ui;

}

// *********************** Public Methods ************************

// Setters

void WidgetConfiguration::setPath(QString path){
    m_ui->lineEditPath->setText(path);
}

// Getters

QString WidgetConfiguration::getPath(){
    return m_ui->lineEditPath->text();
}

void WidgetConfiguration::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}


// ********************* Private Methods ************************

bool WidgetConfiguration::saveNewPath(QString path){
    if(!correctPath(path)){
        QPalette palette;
        palette.setBrush(QPalette::WindowText, Qt::red);
        m_ui->labInfoPath->setPalette(palette);
        m_ui->labInfoPath->setText(tr("Incorrect Path"));

        startTimer(1500);
        return false;
    }else{
        QPalette palette;
        palette.setBrush(QPalette::WindowText, Qt::darkGreen);
        m_ui->labInfoPath->setPalette(palette);
        m_ui->labInfoPath->setText(tr("Correct Path!"));

        // Save the path in the config file
        QDir dir;
        dir.cd(dir.homePath());
        if(!dir.cd(".qtrembinds")){ // If the dir doesn't exists
            dir.mkdir(".qtrembinds"); // create it
            dir.cd(".qtrembinds");
        }
        QFile file(dir.filePath("current_path.txt"));
        file.open(QIODevice::WriteOnly | QIODevice::Text);
        QTextStream out(&file);
        out << path << "\n";
        file.close();

        startTimer(1500);
        return true;
    }
}

bool WidgetConfiguration::correctPath(QString path){
    QDir dir;
    if(!dir.cd(path))
        return false;
    else{
        if(!dir.cd("base"))
            return false;
        else{
            if(!dir.exists("autogen.cfg"))
                return false;
            else
                return true;
        }
    }
}


// SLOTS
void WidgetConfiguration::on_btnOK_clicked()
{
    if(this->saveNewPath(m_ui->lineEditPath->text())){
        emit accepted(); // makes changes
        emit rejected(); // hide this widget
    }
}

void WidgetConfiguration::on_btnApply_clicked()
{
    if(saveNewPath(m_ui->lineEditPath->text())){
        emit accepted(); // Only makes changes
    }
}

void WidgetConfiguration::on_btnCancel_clicked()
{
    emit rejected(); // Exit
}


void WidgetConfiguration::on_btnDefault_clicked()
{
    QDir homePath;
    m_ui->lineEditPath->setText(homePath.homePath() + "/.tremulous");
}

void WidgetConfiguration::on_btnRestore_clicked()
{
    QDir dir;
    dir.cd(dir.homePath());
    if(dir.cd(".qtrembinds")){
        if(dir.exists("current_path.txt")){
            QFile file(dir.filePath("current_path.txt"));
            file.open(QIODevice::ReadOnly | QIODevice::Text);
            QTextStream in(&file);
            QString line = in.readLine();
            setPath(line);
            file.close();
        }
    }
}

void WidgetConfiguration::timerEvent(QTimerEvent *event){
    m_ui->labInfoPath->setText("");
    killTimer(event->timerId());
}

void WidgetConfiguration::on_btnSearchPath_clicked()
{
    QFileDialog fileDialog(this); // Dialog for chose the directory
    fileDialog.setFileMode(QFileDialog::Directory);
    fileDialog.setWindowTitle(tr("Chose directory"));
    fileDialog.setDirectory(m_ui->lineEditPath->text());

    fileDialog.exec();
    if(fileDialog.result() == QDialog::Accepted){
        QStringList lista = fileDialog.selectedFiles();
        m_ui->lineEditPath->setText(lista.takeFirst());
    }
}
